package com.hu.provider;

import cn.hutool.core.net.NetUtil;
import com.hu.RpcApplication;
import com.hu.config.RegistryConfig;
import com.hu.config.RpcConfig;
import com.hu.model.ServiceMetaInfo;
import com.hu.provider.UserServiceImpl;
import com.hu.registry.LocalRegistry;
import com.hu.registry.Registry;
import com.hu.registry.RegistryFactory;
import com.hu.server.HttpServer;
import com.hu.server.VertxHttpServer;
import com.hu.server.tcp.VertxTcpServer;
import com.hu.service.UserService;
import lombok.extern.slf4j.Slf4j;


/**
 * 服务提供者示例
 */
@Slf4j
public class EasyProviderExample {

    public static void main(String[] args) {
        // RPC 框架初始化
        RpcApplication.init();
        log.info("1、RPC 框架初始化完成！");
        // 注册服务
        String serviceName = UserService.class.getName();
        LocalRegistry.register(serviceName, UserServiceImpl.class);
        log.info("2、注册服务初始化完成！");
        // 注册服务到注册中心
        RpcConfig rpcConfig = RpcApplication.getRpcConfig();
        RegistryConfig registryConfig = rpcConfig.getRegistryConfig();
        Registry registry = RegistryFactory.getInstance(registryConfig.getRegistry());
        ServiceMetaInfo serviceMetaInfo = new ServiceMetaInfo();
        serviceMetaInfo.setServiceName(serviceName);
        serviceMetaInfo.setServiceHost(rpcConfig.getServerHost());
        serviceMetaInfo.setServicePort(rpcConfig.getServerPort());
        try {
            registry.register(serviceMetaInfo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        log.info("3、注册服务到注册中心完成！");
        //System.out.println("TCP server started on port ");
        VertxTcpServer vertxTcpServer = new VertxTcpServer();
        vertxTcpServer.doStart(8081);
    }
}
